package com.hydl.power.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.hydl.power.mapper.LogMapper;
import com.hydl.power.pojo.Log;
import com.hydl.power.pojo.LogExample;
import com.hydl.power.service.LogService;
import com.hydl.power.utils.DateTimeUtils;
import com.hydl.power.utils.TableUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class  LogServiceImpl implements LogService {

    @Autowired
    private LogMapper logMapper;

    @Override
    public int deleteByPrimaryKey(String logId) {
        return logMapper.deleteByPrimaryKey(logId);
    }

    @Override
    public int insert(Log record) {
        return logMapper.insert(record);
    }

    @Override
    public int insertSelective(Log record) {
        return logMapper.insertSelective(record);
    }

    @Override
    public Log selectByPrimaryKey(String logId) {
        return logMapper.selectByPrimaryKey(logId);
    }

    @Override
    public int updateByPrimaryKeySelective(Log record) {
        return logMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Log record) {
        return logMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Log> selectAllByCondition(Integer pageSize, Integer pageNum, String startTime, String endTime) {
        PageHelper.startPage(pageNum, pageSize);
        LogExample logExample = new LogExample();
        LogExample.Criteria criteria = logExample.createCriteria();
        criteria.andLogDateBetween(DateTimeUtils.strToDate(startTime), DateTimeUtils.strToDate(endTime));
        logExample.setOrderByClause("log_date");
        return logMapper.selectByExample(logExample);
    }

    @Override
    public TableUtils selectAllByConditions(Integer pageSize, Integer pageNum, String startTime, String endTime) {
        Page<Log> page = PageHelper.startPage(pageNum, pageSize);
        LogExample logExample = new LogExample();
        LogExample.Criteria criteria = logExample.createCriteria();
        if (!Strings.isNullOrEmpty(startTime)) {
            criteria.andLogDateGreaterThanOrEqualTo(DateTimeUtils.strToDate(startTime));
        }
        if (!Strings.isNullOrEmpty(endTime)) {
            criteria.andLogDateLessThan(DateTimeUtils.strToDate(endTime));
        }
        logExample.setOrderByClause("logdate desc");
        List<Log> list = logMapper.selectByExample(logExample);
        TableUtils tu = new TableUtils();
        tu.setTotal(page.getTotal());
        tu.setRows(list);
        return tu;
    }

    @Override
    public PageInfo<Log> getLogList(Integer pageNum, Integer pageSize, String startTime, String endTime) {
       List<Log> list= logMapper.getLogList(pageNum,pageSize,startTime,endTime);
        return new PageInfo<>(list);
    }


}
